using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Foundations.Net.Threading;

namespace Foundations.Net.Pooling
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("=================== Object Pool ===================");
            System.Threading.Thread[] threads = new System.Threading.Thread[1000];
            for (int x = 0; x < threads.Length; x++)
            {
                threads[x] = new System.Threading.Thread(new System.Threading.ThreadStart(Scheduler.Schedule));
                threads[x].Name = x.ToString();
                threads[x].Start();
            }

            Console.ReadLine();

            Console.WriteLine("=================== Metaphore ===================");
            string name = typeof(Program).AssemblyQualifiedName;
            Metaphor main = new Metaphor(name, 2);
            Task.Run(() =>
            {
                var metaphor = new Metaphor(name, 2);
                if (!metaphor.WaitOne())
                {
                    //Simulate work
                    Thread.Sleep(5000);
                    Console.WriteLine("signaling 1...");
                    metaphor--;
                }
            });
            Task.Run(() =>
            {
                var metaphor = new Metaphor(name, 2);
                if (!metaphor.WaitOne())
                {
                    //Simulate work
                    Thread.Sleep(10000);
                    Console.WriteLine("signaling 2...");
                    metaphor--;
                }
            });
            Console.WriteLine("waiting...");
            try
            {
                main.WaitOne(new TimeSpan(0, 0, 15));
                Console.WriteLine("exited!");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.ReadLine();

        }

    }
}
